package com.xunji.user.controller;


import com.xunji.user.common.R;
import com.xunji.user.entity.Org;
import com.xunji.user.entity.User;
import com.xunji.user.service.OrgService;
import com.xunji.user.service.UserService;
import com.xunji.user.vo.req.OrgAddRequest;
import com.xunji.user.vo.req.OrgUpdateRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Api(tags = "组织管理")
@RestController
@RequestMapping("/org")
public class OrgController {

    @Resource
    OrgService orgService;

    @Resource
    private UserService userService;


    @GetMapping("")
    @ApiOperation(value = "查询组织机构", notes = "查询组织机构")
    public R getOrg() {
        return R.success(orgService.getOrgs());
    }

    @PutMapping("")
    @ApiOperation(value = "新增组织机构", notes = "新增组织机构")
    public R addOrg(@RequestBody OrgAddRequest request) {
        if (!StringUtils.hasText(request.getName()) || request.getParentId() == null) {
            return R.failure("参数不合法");
        }
        try {
            // 重名校验
            List<Org> orgs = orgService.selectByName(request.getName());
            if (orgs != null && !orgs.isEmpty()) return R.failure("名称已存在");
            orgService.addOrg(request);
            return R.success();
        } catch (Exception e) {
            log.error("添加组织失败，e->{}", e);
            return R.failure("添加组织失败");
        }
    }

    @PostMapping("")
    @ApiOperation(value = "更新组织机构", notes = "更新组织机构")
    public R updateOrg(@RequestBody OrgUpdateRequest request) {
        try {
            // 参数校验
            if (request == null || request.getId() == null) return R.failure("参数不合法！");

            // 重名校验
            List<Org> orgs = orgService.selectByNameAndNotSelf(request.getName(), request.getId());
            if (orgs != null && !orgs.isEmpty()) return R.failure("名称已存在");
            orgService.updateOrg(request);
            return R.success();
        } catch (Exception e) {
            log.error("更新组织机构失败，e->{}", e);
            return R.failure("更新组织机构失败");
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除组织机构", notes = "删除组织机构")
    public R deleteOrg(@PathVariable("id") Integer id) {
        try {
            // 根节点不可删除，校验是否为删除的根节点
            Org org = orgService.selectById(id);
            if (org == null) return R.failure("组织机构不存在");
            if (org.getPId() == 0) return R.failure("根节点不可删除");
            // 判断是否存在子节点
            List<Org> orgs = orgService.selectByPid(id);
            if (orgs != null && !orgs.isEmpty()) return R.failure("该部门下存在子部门，不可删除");
            // 判断是否存在人员
            List<User> users = userService.selectByOrgId(id);
            if (users != null && !users.isEmpty()) return R.failure("该部门下存在人员，不可删除");
            orgService.deleteById(id);
            return R.success();
        } catch (Exception e) {
            log.error("删除组织机构失败，e->{}", e);
            return R.failure("删除组织机构失败");
        }
    }

}
